package com.wx.springboot.step9back.common.util;

import com.wx.springboot.step9back.model.Logs;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Component
public class WordUtil {

    public void createWord (Logs logs){
        Map<String,Object > dataMap = new HashMap();
        try {
            String fileName = logs.getName() + "-" + logs.getCreateTime() + "-" + logs.getId() + "-";
            // 姓名
            dataMap.put("name", logs.getName());
            // 日期
            dataMap.put("time", logs.getCreateTime());
            // 学习内容
            dataMap.put("content", logs.getContent());
            // 学习收获
            dataMap.put("gains", logs.getGains());
            // 不足
            dataMap.put("lack", logs.getLack());
            // 班主任
            dataMap.put("teacher", logs.getTeacher());
            // 教练
            dataMap.put("coach", logs.getCoach());
            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");

            /**
             * 指定ftl文件所在目录的路径，而不是ftl文件的路径
             */
            //我的路径是D:/word/日报.ftl
            configuration.setDirectoryForTemplateLoading(new File("D:\\word"));

            //输出文档路径及名称
            File outFile = new File("D:\\word\\"+fileName+"日报.doc");

            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate("日报.ftl", "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
